<?php 
/////////////////////////////////////////////////////////////////////////////
// 这个文件是 网城创想多用户商城 项目的一部分
//
// Copyright (c) 2007 - 2008 www.shopnc.net 
//
// 要查看完整的版权信息和许可信息，请查看源代码中附带的 COPYRIGHT 文件，
// 或者访问 http://www.shopnc.net/ 获得详细信息。
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : product.manage.php   FILE_PATH : system\product.manage.php
 * ....商品管理功能
 *
 * @copyright Copyright (c) 2007 - 2007 www.shopnc.net 
 * @author 网城创想多用户商城开发团队 php_netproject@yahoo.com.cn
 * @package 
 * @subpackage 
 * @version Tue Aug 28 15:51:41 CST 2007
 */
require_once("../global.inc.php");

class SysProductManage extends SystemFrameWork{
	/**
	 * 商品对象
	 *
	 * @var obj
	 */
	var $obj_product;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $objvalidate;
	/**
	 * 会员对象
	 *
	 * @var obj
	 */
	var $obj_member;
	/**
	 * 商品分类对象
	 *
	 * @var obj
	 */
	var $objProductCate;
	/**
	 * 网站提醒对象
	 *
	 * @var obj
	 */
	var $obj_remind;

	function main(){
		/**
		 * 创建商品对象
		 */
		if (!is_object($this->obj_product)){
			require_once("product.class.php");
			$this->obj_product = new ProductClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->objvalidate)){
			require_once("commonvalidate.class.php");
			$this->objvalidate = new CommonValidate();
		}

		/**
		 * 设置模板路径
		 */
		$this->setsubtemplates("system");

		/**
		 * 语言包
		 */
		$this->getlang("product");

		switch ($this->_input['action']){
			case "list":
				$this->_listproduct();
				break;
//			case "add":
//				$this->_addproduct();
//				break;
//			case "save":
//				$this->_saveproduct();
//				break;
			case "sell":
				$this->_sellproduct();
				break;
			case "modi":
				$this->_modiproduct();
				break;
			case "update":
				$this->_updateproduct();
				break;
			case "del":
				$this->_delproduct();
				break;
			case "irregularities":
				$this->_irregularities();
				break;
			case "under":
				$this->_updateproductstate();
				break;
			default:
				$this->_listproduct();
				break;
		}

	}
	/**
	 * 商品列表页面
	 *
	 */
	function _listproduct(){

		/**
		 * 初始化分页类
		 */
		require_once("commonpage.class.php");
		$obj_page = new CommonPage();
		/**
		 * 创建会员对象
		 */
		if (!is_object($this->obj_member)){
			require_once("member.class.php");
			$this->obj_member = new MemberClass();
		}
		/**
		 * 创建商品分类对象
		 */
		if (!is_object($this->objProductCate)){
			require_once ("productclass.class.php");
			$this->objProductCate = new ProductCategoryClass();
		}
		/**
		 * 取得查询参数
		 */
		$obj_condition[keygenre] = $this->_input['searchtype'];
		$obj_condition[state] = 1;/*状态 发布*/
		
		$obj_condition[key] = $this->_input['keyword'];
		if("" != $this->_input['pcid']){
			$obj_condition[search_cate] = $this->_input['pcid'];
		}else{
			$obj_condition[search_cate] = $this->_input['searchcate'];
		}
		$obj_condition[havetime] = 1;
		$obj_condition[keygenre] = $this->_input['searchtype'];
		$obj_condition[price_min] = $this->_input['price_min'];
		$obj_condition[price_max] = $this->_input['price_max'];
		if ($this->_input['sell_type'] != 'all'){
			$obj_condition[sell_type] = $this->_input['sell_type'];
		}
		$obj_condition[p_type] = $this->_input['p_type'];
		$obj_condition[search_place] = $this->_input['search_place'];
		$obj_condition[tf_charge] = $this->_input['tf_charge'];
		$obj_condition[type] = $this->_input['type'];
		$obj_condition[end_time] = $this->_input['end_time'];
		$obj_condition[member] = $this->_input['member'];

		if ($this->_input['order'] == ""){
			$obj_condition[order] = 1;
		}else{
			$obj_condition[order] = $this->_input['order'];
		}
		if ($this->_input['sorttype'] == ""){
			$obj_condition[sorttype] = 1;
		}else{
			$obj_condition[sorttype] = $this->_input['sorttype'];
		}
		/**
		 * 取得产品列表
		 */
		$obj_page->pagebarnum(15);
		$product_array = $this->obj_product->getProductList($obj_condition,$obj_page);
		$page_list = $obj_page->show(1);
		
		/**
		 * 得到店主信息
		 */
		$product_array = $this->obj_member->getSomeMember($product_array,'member_id','member_id,login_name as shopmaster');
		
		for($i=0;$i<count($product_array);$i++){
			$left_time = $product_array[$i]['p_end_time'] - time();
			$product_array[$i]['left_days'] = intval($left_time / (24*60*60));
			$product_array[$i]['left_hours'] = intval(($left_time % (24*60*60)) / (60*60));
			$product_array[$i]['left_minutes'] = intval((($left_time % (60*60))) / 60);
		}
		
		/**
		 * 商品分类
		 */
		$ProductCateArray = $this->objProductCate->getOneLevelClass($this->objProductCate->listCateById($id, $deep),0);
		
		
		/**
		 * 输出到页面模板
		 */
		$this->output("p_sell_type", $this->_b_config['p_sell_type']);/*商品交易类型，一口价等*/
		$this->output("page_list", $page_list);
		$this->output("product_array", $product_array);
		$this->_input['sorttype'] = intval(1-$this->_input['sorttype']);
		$this->output("product_condition",$this->_input);
		$this->output('ProductCateArray',$ProductCateArray);
		$this->showpage("sys_product.manage");
	}
	/**
	 * 添加商品页面
	 *
	 */
	function _addproduct(){
		require_once("productclass.class.php");
		$obj_product_category = new ProductCategoryClass();
		$ProductClassArray = $obj_product_category->listClassDetail($conditionstr="",$Objpage);
		$this->output("slPCId",Common::showForm_Select2("slPCId","","",$ProductClassArray,""));
		$this->showpage("sys_product.add");
	}
	/**
	 * 保存商品信息
	 *
	 */
	function _saveproduct(){
		/**
		 * 店铺ID暂时默认为0
		 * 商品状态暂时默认为1
		 */
		$this->_input["txtShopid"] = 0;
		$this->_input["txtPstate"] = 1;
		$this->_input["txtThemeid"] = 1;
		$this->_input["txtPstarttime"] = time();
		$this->_input["txtPendtime"] = mktime(date('H'),date('i'),date('s'),date('m')+3,date('d'),date('Y'));
		/**
		 * 验证表单信息
		 */
		$this->objvalidate->validateparam = array(
		array("input"=>$this->_input["txtPname"],"require"=>"true","message"=>$this->_lang['errProductName']),
		array("input"=>$this->_input["txtPinfo"],"message"=>$this->_lang['errProductInfo']),
		array("input"=>$this->_input["txtPcode"],"require"=>"true","message"=>$this->_lang['errProductCode']),
		array("input"=>$this->_input["txtPunit"],"require"=>"true","message"=>$this->_lang['errProductUnit']),
		array("input"=>$this->_input["txtPprice"],"require"=>"true","validator"=>"Currency","message"=>$this->_lang['errProductPrice']),
		array("input"=>$this->_input["txtPpoint"],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errProductPoint']),
		array("input"=>$this->_input["txtPstorage"],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errProductStorage']),
		array("input"=>$this->_input["txtBrandid"],"validator"=>"Number","message"=>$this->_lang['errProductBrand']),
		array("input"=>$this->_input["txtPviewnum"],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errProductViewNum']));
		$error = $this->objvalidate->validate();

		if($error != ""){
			echo $error;
		}else{
			if(isset($_FILES['txtPpic']['name']) and $_FILES['txtPpic']['name'] != ''){
				require_once("uploadfile.class.php");
				$upload = new UploadFile();
				$upload->allow_type = explode(',',$upload->_fileconfig['allowuploadimagetype']);
				$upload->ifresize = false;
				//$upload->resize_width=50;
				//$upload->resize_height=50;
				$filename = $upload->upfile("txtPpic");
				$this->_input["txtPpic"] = $filename["getfilename"];//上传图片
				unset($upload);
			}
			$result = $this->obj_product->addProduct($this->_input);
			echo "ok";
		}
	}
	/**
	 * 选择商品分类
	 *
	 */
	function _sellproduct(){

		if (!is_object($this->obj_product_cate)){
			require_once("productclass.class.php");
			$this->obj_product_cate = new ProductCategoryClass();
		}
		$product_cate_uid = 0;
		$deep = 1;
		$product_top_cate = $this->obj_product_cate->listCateById($product_cate_uid, $deep);
		$this->output("top_cate", $product_top_cate);
		$this->output("pid", $this->_input['pid']);
		$this->showpage("sys_product.sell");
	}
	/**
	 * 修改产品信息
	 *
	 */
	function _modiproduct(){
		require_once("productclass.class.php");
		$obj_product_category = new ProductCategoryClass();
		$ProductClassArray = $obj_product_category->listClassDetail($conditionstr="", $Objpage);
		$product_array = $this->obj_product->getProductRow($this->_input["pid"]);
		require_once("attribute.class.php");
		$obj_product_attribute = new AttributeClass();
		$pcid = $product_array['pc_id'];
		$product_attribute = $obj_product_attribute->getAttributeList($pcid);
		$product_attribute_content = $obj_product_attribute->getAttributeWithContentList($pcid);

		$attribute_condition_str = " and p_id = '" . $this->_input["pid"] . "'";
		$product_have_attribute = $this->obj_product->getProductAttribute($attribute_condition_str, $obj_page);
		if ($this->_input['select_all'] != ''){
			$select_all_id = explode("|", $this->_input['select_all']);
			$selltype = $this->_input['Selltype']?$this->_input['Selltype']:1;
			$i = 0;
			$cate_name = "";
			foreach($select_all_id as $value){
				$select_row = $obj_product_category->getPcateRow($value);
				$cate_name .= ">>" . $select_row[0][name];
				$i++;
			}
			if("" != $cate_name){
				$cate_name = substr($cate_name, 2);
			}
		}else{
			$cate_row = $obj_product_category->getPcateRow($pcid);
			$cate_name .= ">>" . $cate_row[0][name];
		}

		$i=0;
		if(is_array($product_have_attribute)){
			foreach ($product_have_attribute as $key => $value){

				$ac_content = explode(',', $value[pac_content]);
				foreach ($ac_content as $k => $v){
					$pac_attribute[$i] = $v;
					$i++;
				}
			}
		}

		if(is_array($product_attribute_content)){
			foreach ($product_attribute_content as $key => $value){
				foreach ($value as $k => $v){
					if(is_array($pac_attribute) && in_array($v[ac_id], $pac_attribute)){
						$product_attribute_content[$key][$k][ischecked] = 1;
					}

				}
			}
		}
		if ($this->_input['select_all'] != ''){
			$this->output("slPCId", $select_all_id[count($select_all_id)-1]);
		}else {
			$this->output("slPCId", $product_array['pc_id']);
		}
		$this->output("selltype", $product_array['p_sell_type']);
		$this->output("site_url", $this->_configinfo['websit']['site_url']);
		$this->output("product_array", $product_array);
		$this->output("cate_name", $cate_name);
		$this->output("product_attribute", $product_attribute);
		$this->output("product_attribute_content", $product_attribute_content);
		$this->output("product_have_attribute", $pac_attribute);
		$this->showpage("sys_product.add");
	}
	/**
	 * 更新产品信息
	 *
	 */
	function _updateproduct(){
		/**
		 * 验证表单信息
		 */
		$this->objvalidate->validateparam = array(
		array("input"=>$this->_input["txtPname"],"require"=>"true","message"=>$this->_lang['errProductNameEmpty']),
		array("input"=>$this->_input["txtPinfo"],"require"=>"true","message"=>$this->_lang['errProductInfoEmpty']),
		array("input"=>$this->_input["radioSelltype"],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errSelltype']),
		//array("input"=>$this->_input["slPCId"],"require"=>"true","validator"=>"Integer","message"=>$this->_lang['errPCId']),
		array("input"=>$this->_input["radioType"],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errPType']),
		array("input"=>$this->_input["txtPstorage"],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errPstorage']),
		array("input"=>$this->_input["txtPprice"],"require"=>"true","validator"=>"Currency","message"=>$this->_lang['errPprice']),
		array("input"=>$this->_input["txtGroupprice"],"validator"=>"Currency","message"=>$this->_lang['errGroupprice']),
		array("input"=>$this->_input["txtPoldprice"],"validator"=>"Currency","message"=>$this->_lang['errPoldprice']),
		array("input"=>$this->_input["txtGroupmincount"],"validator"=>"Number","message"=>$this->_lang['errGroupmincount']),
		array("input"=>$this->_input["slProvince"],"require"=>"true","message"=>$this->_lang['errProvince']),
		array("input"=>$this->_input["slCity"],"require"=>"true","message"=>$this->_lang['errCity']),
		array("input"=>$this->_input["radioTransfee"],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errTransfee']),
		array("input"=>$this->_input["radioInvoices"],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errInvoices']),
		array("input"=>$this->_input["radioWarranty"],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errWarranty']),
		array("input"=>$this->_input["slValiddays"],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errValiddays']),
		array("input"=>$this->_input["chxAutopublish"],"validator"=>"Integer","message"=>$this->_lang['errAutopublish']),
		array("input"=>$this->_input["chxRecommended"],"validator"=>"Integer","message"=>$this->_lang['errRecommended']));
		$error = $this->objvalidate->validate();

		if($error != ""){
			echo $error;
		}else{
			$this->_input["txtPinfo"] = Common::replacebr($this->_input["txtPinfo"]);
			$this->_input["txtMemberid"] = $_SESSION['s_login']['id'];
			$this->_input["txtThemeid"] = 0;
			$this->_input["txtPpoint"] = 0;
			if("" == $this->_input["chxAutopublish"]){
				$this->_input["chxAutopublish"] = 0;
			}
			if("" == $this->_input["chxRecommended"]){
				$this->_input["chxRecommended"] = 0;
			}
			if("2" == $this->_input["radioSelltype"]){
				$this->_input["txtPprice"] = $this->_input["txtPoldprice"];
			}

			if(("0" == $this->_input["_now"])){			/*宝贝发布时间立刻开始*/
				if("1" != $this->_input["txtPstate"]){
					$this->_input["txtPstate"] = 1;
					$this->_input["txtPstarttime"] = time();
					$this->_input["txtPendtime"] = Common::calculateDate("d",$this->_input["slValiddays"],$this->_input["txtPstarttime"]);
				}elseif("" != $this->_input["slValiddays"]){	/*宝贝出售结束时间随有效期更改*/
					$this->_input["txtPendtime"] = Common::calculateDate("d",$this->_input["slValiddays"],$this->_input["txtPstarttime"]);
				}

			}elseif ("1" == $this->_input["_now"]){		/*宝贝发布时间设定开始*/
				$this->_input["txtPstate"] = 0;
				$start_time = split("-", $this->_input["_date"]);
				$this->_input["txtPstarttime"] = mktime($this->_input["_hour"],$this->_input["_minute"],0,$start_time[1],$start_time[2],$start_time[0]);
				$this->_input["txtPendtime"] = Common::calculateDate("d",$this->_input["slValiddays"],$this->_input["txtPstarttime"]);

			}elseif ("2" == $this->_input["_now"]){		/*宝贝放入仓库*/
				$this->_input["txtPstate"] = 0;
				$this->_input["txtPstarttime"] = "";
				$this->_input["txtPendtime"] = "";
			}

			if(isset($_FILES['txtPpic']['name']) and $_FILES['txtPpic']['name'] != ''){		/*上传商品图片*/
				require_once("uploadfile.class.php");
				$upload = new UploadFile();
				$upload->allow_type = explode(',',$upload->_fileconfig['allowuploadimagetype']);
				print_r($upload->allow_type);
				$upload->ifresize = false;
				//$upload->resize_width=50;
				//$upload->resize_height=50;
				$filename = $upload->upfile("txtPpic");
				$this->_input["txtPpic"] = $filename["getfilename"];
				$upload->unfile($this->_input["p_pic"]);
				unset($upload);
			}else{
				$this->_input["txtPpic"] = $this->_input["p_pic"];
			}
			
			/**
			 * 判断 商品目录是否被改变
			 */
			$condition['p_code'] = $this->_input['txtPid'];
			$product_array = $this->obj_product->getProductList($condition,&$obj_page);
			if ($product_array[0]['pc_id'] != $this->_input['slPCId']){
				/**
				 * 网站提醒操作
				 */
				if (!is_object($this->obj_remind)){
					require_once('remind.class.php');
					$this->obj_remind = new RemindClass();
				}
				
				$condition['id'] = $product_array[0]['member_id'];
				$member_array = $this->obj_member->getMemberInfo($condition);
		
				$value_array = array();
				$value_array['username'] = $member_array['login_name'];
				$value_array['product_name'] = $product_array[0]['p_name'];
				$this->obj_remind->setMessageOrMail('sale_product_move','product_move',$value_array,$member_array['login_name'],$this->_configinfo);
			}
			$result = $this->obj_product->updateProduct($this->_input);
			/**
		 	 * 商品属性及属性内容ID处理
		 	 */
			if(is_array($this->_input["attribute_content"])){
				for($i = 0; $i < count($this->_input["attribute_content"]); $i++){
					if("" != $this->_input["attribute_content"][$i]){
						$attribute_array[$i] = explode('|', $this->_input["attribute_content"][$i]);
						list($ac_id[$i], $aid[$i]) = $attribute_array[$i];
					}
				}

				$new_aid = array_unique($aid);
				$i=0;
				foreach ($new_aid as $k => $v){
					$insert_aid[$i] = $v;
					$insert_acid[$i] = "";
					foreach($attribute_array as $key => $value){
						if($value[1] == $v){
							$insert_acid[$i] .= ",".$value[0];
						}
					}
					$insert_acid[$i] = preg_replace("/^,/", "", $insert_acid[$i]);
					$insert_ac[$i] = $insert_aid[$i]."|".$insert_acid[$i];
					$i++;
				}
				$result_attribute = $this->obj_product->updateProductAttribute($this->_input["txtPcode"], $insert_ac);
			}
			/**
			 * 更新商品发布数量的统计信息
			 */
			$update_product_statis = $this->obj_product->updateProductStatistics($this->_input["txtMemberid"],'sell');
			
			
			
			
			$this->redirectPath("succ", "/system/product.manage.php?action=list", "商品信息修改成功！");
		}
	}
	/**
	 * 删除商品
	 *
	 */
	function _delproduct(){
		/**
		 * 验证表单信息
		 */
		if (is_array($this->_input['chboxPid'])){
			foreach ($this->_input['chboxPid'] as $value){
				$this->objvalidate->setValidate(array("input"=>$value,"require"=>"true","message"=>$this->_lang['errProductId']));
			}
		}else {
			$this->objvalidate->setValidate(array("input"=>$this->_input['chboxPid'],"require"=>"true","message"=>$this->_lang['errProductId']));
		}
		$error = $this->objvalidate->validate();
		if($error != ""){
			echo $error;
		}else{
			/**
			 * 网站提醒操作
			 */
			if (!is_object($this->obj_remind)){
				require_once('remind.class.php');
				$this->obj_remind = new RemindClass();
			}
			/**
			 * 取商品信息
			 */
			$condition['p_code'] = $this->_input['chboxPid'];
			$condition['state'] = "none";
			$product_array = $this->obj_product->getProductList($condition, &$obj_page);
			
			$condition['id'] = $product_array[0]['member_id'];
			$member_array = $this->obj_member->getMemberInfo($condition);
	
			$value_array = array();
			$value_array['username'] = $member_array['login_name'];
			$value_array['product_name'] = $product_array[0]['p_name'];
			$this->obj_remind->setMessageOrMail('sale_product_del','product_del',$value_array,$member_array['login_name'],$this->_configinfo);
			
			$result = $this->obj_product->delProduct($this->_input['chboxPid']);
			echo "ok";
		}
	}
	
	/**
	 * 商品违规操作
	 */
	function _irregularities(){
		$this->objvalidate->setValidate(array("input"=>$this->_input['pid'],"require"=>"true","message"=>$this->_lang['errProductId']));
		$error = $this->objvalidate->validate();
		if($error != ""){
			echo $error;
			exit;
		}else {
			$result = $this->obj_product->changeProductIrregularities($this->_input,1);
			echo "ok";
		}
	}
	/**
	 * 商品下架操作
	 *
	 */
	function _updateproductstate(){
		/**
		 * 创建会员对象
		 */
		if (!is_object($this->obj_member)){
			require_once("member.class.php");
			$this->obj_member = new MemberClass();
		}

		$this->_input["p_recommended"] = "0";
		/**
		 * 商品下架
		 */
		$update_product_rs = $this->obj_product->changeProductState($this->_input, 0);		
		$recommended = "0";
		
		/**
		 * 网站提醒操作
		 */
		if (!is_object($this->obj_remind)){
			require_once('remind.class.php');
			$this->obj_remind = new RemindClass();
		}
		/**
		 * 取商品信息
		 */
		$condition['p_code'] = $this->_input['chboxPid'];
		$condition['state'] = "none";
		$product_array = $this->obj_product->getProductList($condition, &$obj_page);
		
		$condition['id'] = $product_array[0]['member_id'];
		$member_array = $this->obj_member->getMemberInfo($condition);

		$value_array = array();
		$value_array['username'] = $member_array['login_name'];
		$value_array['product_name'] = $product_array[0]['p_name'];
		$this->obj_remind->setMessageOrMail('sale_product_move','product_down',$value_array,$member_array['login_name'],$this->_configinfo);
		
		/**
		 * 下架商品取消橱窗推荐状态
		 */
		$this->_updateproductrecommended($recommended);

		/**
		 * 更新商品发布数量、推荐商品数量的统计信息
		 */
		$update_product_statis = $this->obj_product->updateProductStatistics($_SESSION['s_login']['id'], 'both');
		echo 'OK';
		exit;
	}
	/**
	 * 更新商品推荐状态
	 *
	 * @param unknown_type $recommended
	 */
	function _updateproductrecommended($recommended){
		$this->objvalidate->validateparam = array(
		array("input"=>$this->_input["chboxPid"],"require"=>"true","message"=>$this->_lang['errProductCodeEmpty']));
		$error = $this->objvalidate->validate();
		if($error != ""){
			echo $error;
		}else{
			$this->_input['recommended'] = $recommended;
			$result = $this->obj_product->updateProductRecommended($this->_input);
			/**
			 * 更新推荐商品数量的统计信息
			 */
			$update_product_statis = $this->obj_product->updateProductStatistics($_SESSION['s_login']['id'], 'recommend');

			if($recommended == "1"){
				$info = "商品已成功推荐！";
			}else{
				$info = "商品已取消推荐！";
			}
			return true;
		}
	}
}

$product_manage = new SysProductManage();
$product_manage->main();
unset($product_manage);
?>